package com.cn.medicalcare.controller.servlet.superadmin;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.Admin;
import com.cn.medicalcare.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/superadmin/updataadmin.json")
public class AdminUpdataServlet extends HttpServlet {

    private AdminService adminService=AdminService.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id;
        String role=req.getParameter("role");
        int survival;
        Map<String,String> map=new LinkedHashMap<>();
        PrintWriter pw=resp.getWriter();
        try {
            id=Integer.valueOf(req.getParameter("id"));
            survival=Integer.valueOf(req.getParameter("survival"));
            if(adminService.updataAdmin(new Admin().setId(id).setRole(role).setSurvival(survival))){
                map.put("status","200");
            }else {
                map.put("status","500");
            }
        }catch (Exception e){
            map.put("status","400");
        }
        pw.print(JsonTool.toJson(map));
    }
}
